import java.util.Arrays;

public class Solution719 {
    public int smallestDistancePair(int[] nums, int k) {
        Arrays.sort(nums);
        int l=0,r=nums[nums.length-1]-nums[0],a=-1;
        while (l<=r){
            int mid=(l+r)/2;
            int j=0,ans=0;
            for (int i=0;i<nums.length;i++){
                while (nums[i]-nums[j]>mid){
                    j++;
                }
                ans+=i-j;
            }
            if (ans>=k){
                a=mid;
                r=mid-1;
            }else {
                l=mid+1;
            }
        }
        return a;
    }

    public static void main(String[] args) {
        System.out.println(new Solution719().smallestDistancePair(new int[]{1,3,10},2));
    }
}
